package controller

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/robfig/cron/v3"
)

func StartTimerTask() error {
	c := cron.New()
	//Warehouse, organization, and owner correspondence timing task
	IdGwController, gErr := c.AddFunc(beego.AppConfig.String("timedtask::GwController"), GwController)
	if gErr != nil {
		logs.Error("add GwController task error:", gErr.Error())
		return gErr
	}
	logs.Info("Add GwController task success, ID is:", IdGwController)

	IdRunTableTask, rErr := c.AddFunc(beego.AppConfig.String("timedtask::RunTableTask"), RunTableTask)
	if rErr != nil {
		logs.Error("add RunTableTask task error:", rErr.Error())
		return rErr
	}
	logs.Info("Add RunTableTask task success, ID is:", IdRunTableTask)

	//Error data export and email sending timing task
	IdToExcelController, tErr := c.AddFunc(beego.AppConfig.String("timedtask::ToExcelController"), ToExcelController)
	if tErr != nil {
		logs.Error("add ToExcelController task error:", tErr.Error())
		return tErr
	}
	logs.Info("Add ToExcelController task success, ID is:", IdToExcelController)

	//Package name correspondence relationship timing task
	IdRunMappingTask, mErr := c.AddFunc(beego.AppConfig.String("timedtask::RunMappingTask"), RunMappingTask)
	if mErr != nil {
		logs.Error("add RunMappingTask task error:", mErr.Error())
		return mErr
	}
	logs.Info("Add RunMappingTask task success, ID is:", IdRunMappingTask)

	//Delete ,iles timed tasks that are more than one month old
	IdRunDelFileTask, dErr := c.AddFunc(beego.AppConfig.String("timedtask::RunDelFileTask"), RunDelFileTask)
	if dErr != nil {
		logs.Error("add RunDelFileTask task error:", dErr.Error())
		return dErr
	}
	logs.Info("Add RunDelFileTask task success, ID is:", IdRunDelFileTask)

	//issue record email feedback
	IdRunIssueRecordTask, iErr := c.AddFunc(beego.AppConfig.String("timedtask::RunIssueRecordTask"), RunIssueRecordTask)
	if iErr != nil {
		logs.Error("add RunIssueRecordTask task error:", iErr.Error())
		return iErr
	}
	logs.Info("Add RunIssueRecordTask task success, ID is:", IdRunIssueRecordTask)

	//analysis the table package whitelist
	IdRunWhitelistTask, wErr := c.AddFunc(beego.AppConfig.String("timedtask::RunWhitelistTask"), RunWhitelistTask)
	if wErr != nil {
		logs.Error("add RunWhitelistTask task error:", wErr.Error())
		return wErr
	}
	logs.Info("Add RunWhitelistTask task success, ID is:", IdRunWhitelistTask)

	//parsing the table package_committer_excels
	IdRunPackageCommitterTask, cErr := c.AddFunc(beego.AppConfig.String("timedtask::RunPackageCommitterTask"), RunPackageCommitterTask)
	if cErr != nil {
		logs.Error("add RunPackageCommitterTask task error:", cErr.Error())
		return cErr
	}
	logs.Info("Add RunPackageCommitterTask task success, ID is:", IdRunPackageCommitterTask)

	//Complete the template information of the issue with the data on the CVE official website
	IdSupplementCveTask, sErr := c.AddFunc(beego.AppConfig.String("timedtask::SupplementCveTask"), SupplementCveTask)
	if sErr != nil {
		logs.Error("add SupplementCveTask task error:", sErr.Error())
		return sErr
	}
	logs.Info("Add SupplementCveTask task success, ID is:", IdSupplementCveTask)

	IdLongSupplementCveTask, lErr := c.AddFunc(beego.AppConfig.String("timedtask::LongSupplementCveTask"), LongSupplementCveTask)
	if lErr != nil {
		logs.Error("add LongSupplementCveTask task error:", lErr.Error())
		return lErr
	}
	logs.Info("Add LongSupplementCveTask task success, ID is:", IdLongSupplementCveTask)
	//Parse opengauss yaml file
	IdParseOpenGaussYamlTask, pErr := c.AddFunc(beego.AppConfig.String("timedtask::ParseOpenGaussYamlTask"), ParseOpenGaussYamlTask)
	if pErr != nil {
		logs.Error("add ParseOpenGaussYamlTask task error:", pErr.Error())
		return pErr
	}
	logs.Info("Add ParseOpenGaussYamlTask task success, ID is:", IdParseOpenGaussYamlTask)

	//Parse the yaml file of mindspore
	IdParseMindsporeYamlTask, yErr := c.AddFunc(beego.AppConfig.String("timedtask::ParseMindsporeYamlTask"), ParseMindsporeYamlTask)
	if yErr != nil {
		logs.Error("add ParseMindsporeYamlTask task error:", yErr.Error())
		return yErr
	}
	logs.Info("Add ParseMindsporeYamlTask task success, ID is:", IdParseMindsporeYamlTask)

	//Parse the yaml file of mindspore
	IdParseOpenlookengYamlTask, oErr := c.AddFunc(beego.AppConfig.String("timedtask::ParseOpenlookengYamlTask"), ParseOpenlookengYamlTask)
	if oErr != nil {
		logs.Error("add ParseMindsporeYamlTask task error:", oErr.Error())
		return oErr
	}
	logs.Info("Add ParseMindsporeYamlTask task success, ID is:", IdParseOpenlookengYamlTask)
	c.Start()
	return nil
}
